package games.ThunderFighter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * Created by Administrator on 2016/12/13.
 */
public class Bullet implements ActionListener,KeyListener {

    GameWindow window;
    static JButton primaryBullet[]=new JButton[20];
    static JButton enemyBullet[]=new JButton[20];
    static Timer time1,time2,time3;
    static int bulletCount=-1,enemybulletCount=-1;
    static int Xtrack[]=new int[20];
    static int Ytrack[]=new int[20];
    static int Wtrack[]=new int[20];
    static int Ztrack[]=new int[20];
    static JButton drop[]=new JButton[3];
    static int dropCout=-1;
    static int Xdroptrack[]=new int[3];
    static int Ydroptrack[]=new int[3];
    static int direction[]=new int[3];

   /*            构造方法    构造方法     构造方法    构造方法    构造方法                    */
    Bullet(GameWindow window){
        int i;
        this.window=window;
        time3=new Timer(30,this);
        for(i=0;i<=2;i++){
            drop[i]=new JButton();
            drop[i].setBackground(Color.green);
            window.panel.add(drop[i]);
            Xdroptrack[i]=1000; Ydroptrack[i]=1000;
            drop[i].setBounds(Xdroptrack[i],Ydroptrack[i],8,8);
            drop[i].addActionListener(this);
        }
    }
     /*            构造方法    构造方法     构造方法    构造方法    构造方法                   */




     /*           掉落物       掉落物       掉落物       掉落物       掉落物       */

    public static void getDrop(){
        if(Math.random()*100<=100){
            dropCout++;
            if(dropCout==3){
                dropCout=0;
            }
            Xdroptrack[dropCout]=Enemy.x;  Ydroptrack[dropCout]=Enemy.y;
            drop[dropCout].setBounds(Xdroptrack[dropCout],Ydroptrack[dropCout],8,8);
            if(Xdroptrack[dropCout]<=230){
                direction[dropCout]=0;
            }
            else{
                direction[dropCout]=1;
            }
            time3.start();
        }
    }
     /*           掉落物       掉落物       掉落物       掉落物       掉落物       */



     /*     添加我方子弹      添加我方子弹      添加我方子弹      添加我方子弹      */

    public void getBullet(){
        time1=new Timer(2,this);
        for(int i=0;i<=19;i++){
            primaryBullet[i]=new JButton();
            primaryBullet[i].addActionListener(this);
            window.panel.add(primaryBullet[i]);
            primaryBullet[i].setBackground(Color.RED);
            primaryBullet[i].setBounds(1000,1000,8,8);
            Xtrack[i]=1000;
            Ytrack[i]=1000;
        }
    }
    /*     添加我方子弹      添加我方子弹      添加我方子弹      添加我方子弹      */




    /*         初级子弹       初级子弹     初级子弹        初级子弹        */
    public static void primaryBulletAttack(int x,int y){
        bulletCount++;
        if(bulletCount==20){
            bulletCount=0;
        }
        Xtrack[bulletCount]=x+25;
        Ytrack[bulletCount]=y-10;

        primaryBullet[bulletCount].setBounds(Xtrack[bulletCount],Ytrack[bulletCount],8,8);
        time1.start();
    }
      /*         初级子弹       初级子弹     初级子弹        初级子弹        */


      /*      中级子弹      中级子弹        中级子弹        中级子弹        */
    public static void middleBulletAttack(int x,int y){
        bulletCount++;
        if(bulletCount==20){
            bulletCount=0;
        }
        Xtrack[bulletCount]=x+15;
        Ytrack[bulletCount]=y-10;

        primaryBullet[bulletCount].setBounds(Xtrack[bulletCount],Ytrack[bulletCount],8,8);

        bulletCount++;
        if(bulletCount==20){
            bulletCount=0;
        }
        Xtrack[bulletCount]=x+35;
        Ytrack[bulletCount]=y-10;

        primaryBullet[bulletCount].setBounds(Xtrack[bulletCount],Ytrack[bulletCount],8,8);
    }
    /*      中级子弹      中级子弹        中级子弹        中级子弹        */



    /*      高级子弹         高级子弹        高级子弹        高级子弹       */
    public static void highBulletAttack(int x,int y){
        bulletCount++;
        if(bulletCount==20){
            bulletCount=0;
        }
        Xtrack[bulletCount]=x+10;
        Ytrack[bulletCount]=y-10;

        primaryBullet[bulletCount].setBounds(Xtrack[bulletCount],Ytrack[bulletCount],8,8);

        bulletCount++;
        if(bulletCount==20){
            bulletCount=0;
        }
        Xtrack[bulletCount]=x+40;
        Ytrack[bulletCount]=y-10;

        primaryBullet[bulletCount].setBounds(Xtrack[bulletCount],Ytrack[bulletCount],8,8);

        bulletCount++;
        if(bulletCount==20){
            bulletCount=0;
        }
        Xtrack[bulletCount]=x+25;
        Ytrack[bulletCount]=y-10;

        primaryBullet[bulletCount].setBounds(Xtrack[bulletCount],Ytrack[bulletCount],8,8);
    }
    /*      高级子弹         高级子弹        高级子弹        高级子弹       */




      /*        敌机子弹        敌机子弹        敌机子弹        敌机子弹*/

    public void enemyBullet(){
        time2=new Timer(8,this);
        for(int i=0;i<=19;i++){
            enemyBullet[i]=new JButton();
            enemyBullet[i].addActionListener(this);
            window.panel.add(enemyBullet[i]);
            enemyBullet[i].setBackground(Color.RED);
            enemyBullet[i].setBounds(1000,1000,8,8);
            Wtrack[i]=1000;
            Ztrack[i]=1000;
        }
    }
    public static void enemyBulletAttack(int x,int y){
        enemybulletCount++;
        if(enemybulletCount==20){
            enemybulletCount=0;
        }
        Wtrack[enemybulletCount]=x+25;
        Ztrack[enemybulletCount]=y+10;

        primaryBullet[enemybulletCount].setBounds(Xtrack[enemybulletCount],Ytrack[enemybulletCount],8,8);
        time2.start();
    }
    /*        敌机子弹        敌机子弹        敌机子弹        敌机子弹*/



    public void actionPerformed(ActionEvent e){
        if(e.getSource()==time1){
            for(int i=0;i<=19;i++){
                Ytrack[i]=Ytrack[i]-1;
                //y=Ytrack[i];
                if(Ytrack[i]<0){
                    primaryBullet[i].setBounds(1000,1000,8,8);
                }
                else{
                primaryBullet[i].setBounds(Xtrack[i],Ytrack[i],8,8);
                }
            }
        }
        if(e.getSource()==time2){
            for(int i=0;i<=19;i++){
                Ztrack[i]=Ztrack[i]+1;
                //y=Ytrack[i];
                if(Ztrack[i]>600){
                    enemyBullet[i].setBounds(1000,1000,8,8);
                }
                else{
                    enemyBullet[i].setBounds(Wtrack[i],Ztrack[i],8,8);
                }
            }
        }
        if(e.getSource()==time3){
            for(int i=0;i<=2;i++){
                if(direction[i]==0){
                    Xdroptrack[i]=Xdroptrack[i]-2;
                    Ydroptrack[i]=Ydroptrack[i]+1;
                    drop[i].setBounds(Xdroptrack[i],Ydroptrack[i],8,8);
                    if(Xdroptrack[i]<=0){
                        direction[i]=1;
                    }
                }
                else{
                    Xdroptrack[i]=Xdroptrack[i]+2;
                    Ydroptrack[i]=Ydroptrack[i]+1;
                    drop[i].setBounds(Xdroptrack[i],Ydroptrack[i],8,8);
                    if(Xdroptrack[i]>=380&&Xdroptrack[i]<=400){
                        direction[i]=0;
                    }
                }
            }
        }
    }
    public void keyPressed(KeyEvent e){}
    public void keyReleased(KeyEvent e){}
    public void keyTyped(KeyEvent e){}

}
